/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.com.intesal.general.controlador;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import sv.com.intesal.general.bean.Sucursal;
import sv.com.intesal.clases.abstracts.ControladorAbstract;
import sv.com.intesal.general.sql.SQLSucursal;

/**
 *
 * @author ejerez
 */
public class SucursalControlador extends ControladorAbstract {

    public void insert(Sucursal s) throws Exception {
        try {
            super.setPreparedStatement(SQLSucursal.insert);
            super.ps.setString(1, s.getNombre_s());
            super.ps.setString(2, super.v.getEstado(s.getEstado_filtro_s()));
            super.ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public void update(Sucursal s) throws Exception {
        try {
            super.setPreparedStatement(SQLSucursal.update);
            super.ps.setString(1, s.getNombre_s());
            super.ps.setString(2, super.v.getEstado(s.getEstado_filtro_s()));
            super.ps.setInt(3, s.getCodigo_s());
            super.ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public List<Sucursal> getSucursal() throws Exception {
        List<Sucursal> lista = new ArrayList<Sucursal>();
        try {
            this.getExecuteQuery(SQLSucursal.getSucursal);
            while (super.rs.next()) {
                lista.add(this.getSucursal(super.rs));
            }
            return lista;
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public List<Sucursal> getSucursalActivos() throws Exception {
        List<Sucursal> lista = new ArrayList<Sucursal>();
        try {
            this.getExecuteQuery(SQLSucursal.getSucursalActivos);
            while (super.rs.next()) {
                lista.add(this.getSucursal(super.rs));
            }
            return lista;
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public Sucursal getSucursal(Integer codigo_s) throws Exception {
        try {
            super.setPreparedStatement(SQLSucursal.getSucursal_x_Codigo);
            super.ps.setInt(1, codigo_s);
            super.getExecuteQuery(ps);

            while (super.rs.next()) {
                return this.getSucursal(super.rs);
            }
            return new Sucursal();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    private Sucursal getSucursal(ResultSet rs) throws Exception {
        try {
            Sucursal s = new Sucursal();
            s.setCodigo_s(rs.getInt("codigo_s"));
            s.setNombre_s(rs.getString("nombre_s"));
            s.setEstado_s(rs.getString("estado_s"));
            s.setEstado_filtro_s(v.getEstado(rs.getString("estado_s")));
            return s;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }
}
